package cn.nosql.tools;

import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class MyDefindCache {

    public static void addDb(String dbName, Hashtable<String, String> ht)
    {
        String fileName = MyDefindCache.class.getProtectionDomain().getCodeSource().getLocation().toString() + "my_store.db";
        //FileUtils.delete(fileName);
        //System.out.println(fileName);
        MVStore store = new MVStore.Builder().fileName(fileName).open();
        MVMap<String, Hashtable<String, String>> m = store.openMap("my_data");
        if (!m.containsKey(dbName)) {
            m.put(dbName, ht);
        }
        store.commit();
        store.close();
    }

    public static MVStore getStore(String dbName)
    {
        String fileName = MyDefindCache.class.getProtectionDomain().getCodeSource().getLocation().toString() + "my_store.db";
        //FileUtils.delete(fileName);
        //System.out.println(fileName);
        MVStore store = new MVStore.Builder().fileName(fileName).open();
        MVMap<String, Hashtable<String, String>> m = store.openMap("my_data");
        Hashtable<String, String> ht = m.get(dbName);
        store.close();

        MVStore.Builder builder = definDb(ht);
        return builder.open();
    }

    public static MVStore.Builder definDb(final Hashtable<String, String> ht)
    {
        MVStore.Builder builder = new MVStore.Builder();
        if (ht != null)
        {
            if (ht.containsKey("fileName"))
            {
                builder.fileName(ht.get("fileName"));
            }

            if (ht.containsKey("encryptionKey"))
            {
                builder.encryptionKey(ht.get("encryptionKey").toCharArray());
            }

            if (ht.containsKey("compress") && (ht.get("compress").equals("true") || ht.get("compress").equals("1")))
            {
                builder.compress();
            }
        }

        return builder;
    }

    public static void setData(final String cache_name, final List<Byte> my_key, final List<Byte> my_value)
    {
        MVStore store = MyDefindCache.getStore(cache_name);
        MVMap map = store.openMap(cache_name + "_cache");
        map.put(MyConvertUtil.restoreToObject(my_key), MyConvertUtil.restoreToObject(my_value));
        store.commit();
        store.close();
    }

//    public static Object getData(final String cache_name, final ByteBuffer my_key)
//    {
//        MVStore store = MyDefindCache.getStore(cache_name);
//        MVMap map = store.openMap(cache_name + "_cache");
//        Integer key = (Integer) MyConvertUtil.toObj(my_key);
//        Object rs = map.get(key);
//        store.close();
//        return rs;
//    }

    public static Object getData(final String cache_name, final Object my_key)
    {
        MVStore store = MyDefindCache.getStore(cache_name);
        MVMap map = store.openMap(cache_name + "_cache");
        Object rs = map.get(my_key);
        store.close();
        //System.out.println(my_key);
        //System.out.println(rs);
        return rs;
    }

    public static void msetData(final String cache_name, final List my_lst)
    {
        System.out.println(cache_name);
        MVStore store = MyDefindCache.getStore(cache_name);
        MVMap map = store.openMap(cache_name + "_cache");
        //map.put(MyConvertUtil.toObj(my_lst), MyConvertUtil.toObj(my_value));
        //System.out.println(my_lst);
        //Integer my_key = 0;
        for (int i = 0; i < my_lst.size(); i = i + 2)
        {
            //my_key = (Integer) my_lst.get(i);
            map.put(my_lst.get(i), my_lst.get(i + 1));
            //System.out.println(my_lst.get(i) + " " + my_lst.get(i + 1).toString());
        }
        store.commit();
        store.close();

//        Object rs = map.get(my_key);
//        System.out.println("**************");
//        System.out.println(rs);
    }

    public static List mgetData(final String cache_name, final List my_lst)
    {
        List lst = new ArrayList();
        MVStore store = MyDefindCache.getStore(cache_name);
        MVMap map = store.openMap(cache_name + "_cache");
        for (int i = 0; i < my_lst.size(); i++)
        {
            lst.add(map.get(my_lst.get(i)));
        }
        store.close();
        return lst;
    }
}






































